home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 08 - 1992 / 08.02 Jun 92 / Modifying Print Dialogs / NoDraft.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-26  |  6.8 KB  |  335 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------
  2. #
  3. #    MultiFinder-Aware Simple Application
  4. #    to modify Print Dialogs
  5. #
  6. #    NoDraft
  7. #
  8. #    NoDraft.c    -    C Source
  9. #
  10. #    NoDraft is an example application that 
  11. #    demonstrates how to modify Print Dialogs.
  12. #
  13. ------------------------------------------*/
  14.  
  15. #include <Values.h>
  16. #include <Types.h>
  17. #include <Resources.h>
  18. #include <QuickDraw.h>
  19. #include <Fonts.h>
  20. #include <Events.h>
  21. #include <Windows.h>
  22. #include <Menus.h>
  23. #include <TextEdit.h>
  24. #include <Desk.h>
  25. #include <ToolUtils.h>
  26. #include <Memory.h>
  27. #include <SegLoad.h>
  28. #include <OSUtils.h>
  29. #include <OSEvents.h> 
  30. #include <DiskInit.h>
  31. #include <Packages.h>
  32. #include <Printing.h>
  33. #include <Traps.h>
  34. #include <NoDraft.h>
  35. #include <NoDraft_types.h>
  36.  
  37. WindowPtr    myWindow;
  38.  
  39. SysEnvRec    gMac;
  40. Boolean        gHasWaitNextEvent;
  41. Boolean        gInBackground;
  42.  
  43. void    EventLoop( void );
  44. void     DoEvent( EventRecord *event );
  45. void     DoUpdate( WindowPtr window );
  46. void     DoActivate( WindowPtr window, Boolean becomingActive );
  47. void    DrawWindow( WindowPtr window );
  48. void     AdjustMenus( void );
  49. void     DoMenuCommand( long menuResult );
  50. void     Initialize( void );
  51. Boolean IsAppWindow( WindowPtr window );
  52. Boolean IsDAWindow( WindowPtr window );
  53. Boolean TrapAvailable( short tNumber, TrapType tType );
  54. void     AlertUser( short error );
  55.  
  56. void ShowAboutMeDialog(void) extern;     /* my about… dialog */
  57. OSErr DoPrintDialog() extern;
  58.  
  59. extern void _DataInit();
  60.  
  61. #pragma segment Main
  62. main()
  63. {
  64.     UnloadSeg((Ptr) _DataInit);
  65.     MaxApplZone();
  66.     Initialize();
  67.     UnloadSeg((Ptr) Initialize);
  68.     EventLoop();
  69. }
  70.  
  71. void EventLoop()
  72. {
  73.     RgnHandle    cursorRgn;
  74.     Boolean        gotEvent;
  75.     EventRecord    event;
  76.  
  77.     cursorRgn = NewRgn();
  78.     do {
  79.         /* use WNE if it is available */
  80.         if ( gHasWaitNextEvent ) {
  81.             SetCursor(&qd.arrow);
  82.             gotEvent = WaitNextEvent(everyEvent, &event, MAXLONG, cursorRgn);
  83.         }
  84.         else {
  85.             SystemTask();
  86.             gotEvent = GetNextEvent(everyEvent, &event);
  87.         }
  88.         if ( gotEvent ) {
  89.             /* we just need the arrow cursor */
  90.             SetCursor(&qd.arrow);
  91.             DoEvent(&event);
  92.         }
  93.     } while ( true );    /* quit via ExitToShell() */
  94. } /*EventLoop*/
  95.  
  96. void DoEvent(event)
  97.     EventRecord    *event;
  98. {
  99.     short        part;
  100.     WindowPtr    window;
  101.  
  102.     switch ( event->what ) {
  103.         case mouseDown:
  104.             part = FindWindow(event->where, &window);
  105.             switch ( part ) {
  106.                 case inMenuBar:
  107.                     AdjustMenus();
  108.                     DoMenuCommand(MenuSelect(event->where));
  109.                     break;
  110.                 case inSysWindow:
  111.                     SystemClick(event, window);
  112.                     break;
  113.             }
  114.             break;
  115.         case activateEvt:
  116.             DoActivate((WindowPtr) event->message, (event->modifiers & activeFlag) != 0);
  117.             break;
  118.         case updateEvt:
  119.             DoUpdate((WindowPtr) event->message);
  120.             break;
  121.         case kOSEvent:
  122.             switch ((event->message >> 24) & 0x0FF) {
  123.                 case kSuspendResumeMessage:
  124.                     gInBackground = (event->message & kResumeMask) == 0;
  125.                     DoActivate(FrontWindow(), !gInBackground);
  126.                     break;
  127.             }
  128.             break;
  129.     }
  130. } /*DoEvent*/
  131.  
  132. void DoUpdate(window)
  133.     WindowPtr    window;
  134. {
  135.     if ( IsAppWindow(window) ) {
  136.         BeginUpdate(window);
  137.         if ( ! EmptyRgn(window->visRgn) )
  138.             DrawWindow(window);
  139.         EndUpdate(window);
  140.     }
  141. } /*DoUpdate*/
  142.  
  143. void DoActivate(window, becomingActive)
  144.     WindowPtr    window;
  145.     Boolean        becomingActive;
  146. {
  147. Boolean            ismine;
  148.  
  149.     ismine = (window == myWindow) ? true : false;
  150.     if ( IsAppWindow(window) ) {
  151.         if ( becomingActive )
  152.             {     
  153.                 SetPort(window);
  154.                 DrawMenuBar();
  155.             }
  156.     } /* of isappwindow */
  157. } /*DoActivate*/
  158.  
  159. void DrawWindow(window)
  160.     WindowPtr    window;
  161. {
  162.  
  163.     SetPort(window);
  164.     EraseRect(&window->portRect);
  165.     SetOrigin(0, 0);
  166.     DrawGrowIcon(window);
  167.  
  168. } /*DrawWindow*/
  169.  
  170. void AdjustMenus()
  171. {
  172.     WindowPtr    window;
  173.     MenuHandle    menu;
  174.  
  175.     window = FrontWindow();
  176.  
  177.     menu = GetMHandle(mFile);
  178.     if ( IsDAWindow(window) )
  179.         {
  180.             EnableItem(menu, iClose);
  181.             /* but no printing */
  182.             DisableItem(menu, iPSetUp);
  183.             DisableItem(menu, iPrint);
  184.         }
  185.     else
  186.         {
  187.             DisableItem(menu, iClose);    
  188.             DisableItem(menu, iPSetUp);
  189.             EnableItem(menu, iPrint);
  190.         }
  191.  
  192. } /*AdjustMenus*/ 
  193.  
  194. void DoMenuCommand(menuResult)
  195.     long        menuResult;
  196. {
  197.     short        menuID;
  198.     short        menuItem;
  199.     Str255        daName;
  200.     short        daRefNum;
  201.     Boolean        handledByDA;
  202.  
  203.     menuID = HiWord(menuResult);
  204.     menuItem = LoWord(menuResult);
  205.     switch ( menuID ) {
  206.         case mApple:
  207.             switch ( menuItem ) {
  208.                 case iAbout:        /* alert for About */
  209.                     /* do filtered dialog */
  210.                     ShowAboutMeDialog();
  211.                     break;
  212.                 default:
  213.                     GetItem(GetMHandle(mApple), menuItem, daName);
  214.                     daRefNum = OpenDeskAcc(daName);
  215.                     break;
  216.             }
  217.             break;
  218.         case mFile:
  219.             switch ( menuItem ) {
  220.                 case iPrint:
  221.                         /* do Print job dialog */
  222.                         /* the Draft Mode is disabled */
  223.                         /* and a filter is added */
  224.                         PrOpen();
  225.                         DoPrintDialog();
  226.                         PrClose();
  227.                         break;
  228.                 case iQuit:
  229.                     ExitToShell();   
  230.                     break;  
  231.             }
  232.             break;
  233.         case mEdit:
  234.             handledByDA = SystemEdit(menuItem-1);
  235.             break;
  236.     }
  237.     HiliteMenu(0);
  238. } /*DoMenuCommand*/
  239.  
  240. #pragma segment Initialize
  241. void Initialize()
  242. {
  243.     Handle        menuBar;
  244.     long        total, contig;
  245.     EventRecord event;
  246.     short        count;
  247.     Boolean        good;
  248.  
  249.     gInBackground = false;
  250.     InitGraf((Ptr) &qd.thePort);
  251.     InitFonts();
  252.     InitWindows();
  253.     InitMenus();
  254.     TEInit();
  255.     InitDialogs(nil);
  256.     InitCursor();
  257.     for (count = 1; count <= 3; count++)
  258.         EventAvail(everyEvent, &event);
  259.     SysEnvirons(kSysEnvironsVersion, &gMac);
  260.     if (gMac.machineType < 0) AlertUser(eUnknown);
  261.     gHasWaitNextEvent = TrapAvailable(_WaitNextEvent, ToolTrap);
  262.     if ((long) GetApplLimit() - (long) ApplicZone() < kMinHeap) AlertUser(eUnknown);
  263.     PurgeSpace(&total, &contig);
  264.     if (total < kMinSpace) AlertUser(eUnknown);
  265.     good = false;
  266.     myWindow = GetNewWindow(rWindow, nil, (WindowPtr) -1);
  267.     good = myWindow != nil;
  268.     if ( good ) {
  269.             SetPort(myWindow);
  270.             ShowWindow(myWindow);
  271.     } else {
  272.         AlertUser(eNoWindow);
  273.         ExitToShell();
  274.     }
  275.     menuBar = GetNewMBar(rMenuBar);
  276.     if ( menuBar == nil ) AlertUser(eUnknown);
  277.     SetMenuBar(menuBar);
  278.     DisposHandle(menuBar);
  279.     AddResMenu(GetMHandle(mApple), 'DRVR');
  280.     DrawMenuBar();
  281.     
  282. } /*Initialize*/
  283.  
  284. #pragma segment Main
  285. Boolean IsAppWindow(window)
  286.     WindowPtr    window;
  287. {
  288.     short        windowKind;
  289.  
  290.     if ( window == nil )
  291.         return false;
  292.     else {
  293.     /* app windows have windowKinds = userKind (8) */
  294.         windowKind = ((WindowPeek) window)->windowKind;
  295.         return (windowKind = userKind);
  296.     }
  297. } /*IsAppWindow*/
  298.  
  299. Boolean IsDAWindow(window)
  300.     WindowPtr    window;
  301. {
  302.     if ( window == nil )
  303.         return false;
  304.     else    /* DA windows have -'ve windowKinds */
  305.         return ((WindowPeek) window)->windowKind < 0;
  306. } /*IsDAWindow*/
  307.  
  308. #pragma segment Initialize
  309. Boolean TrapAvailable(tNumber,tType)
  310.     short        tNumber;
  311.     TrapType    tType;
  312. {
  313.     if ( ( tType == ToolTrap ) &&
  314.         ( gMac.machineType > envMachUnknown ) &&
  315.         ( gMac.machineType < envMacII ) ) {
  316.         tNumber = tNumber & 0x03FF;
  317.         if ( tNumber > 0x01FF )
  318.             tNumber = _Unimplemented;
  319.     }
  320.     return NGetTrapAddress(tNumber, tType) != GetTrapAddress(_Unimplemented);
  321. } /*TrapAvailable*/
  322.  
  323. #pragma segment Main
  324. void AlertUser(error)
  325.     short        error;
  326. {
  327.     short        itemHit;
  328.     Str255        message;
  329.  
  330.     SetCursor(&qd.arrow);
  331.     GetIndString(message, kErrStrings, error);
  332.     ParamText(message, "", "", "");
  333.     itemHit = Alert(rUserAlert, nil);
  334. } /* AlertUser */
  335.